﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Utilities
{
    public static class UriExtensions
    {
        public static Uri ReplaceTopLevelDomain(this Uri url, string oldDomain, string newDomain)
        {
            var host = url.Host;

            if (host.ToLower().EndsWith(oldDomain))
            {
                int len = oldDomain.Length;
                host = host.Remove(host.Length - len) + newDomain;

                var sb = new StringBuilder();
                sb.Append("http://");
                sb.Append(host);
                sb.Append(url.PathAndQuery);

                return new Uri(sb.ToString());
            }

            return url;
        }
    }
}
